<?php

declare(strict_types=1);

/**
 * @Author:     Frank
 * @contact     frank_hhm@163.com
 * @Date:       2023-02-23 11:04:40
 * @Last Modified time: 2023-02-23 11:42:32
 */
namespace app\sys\controller;	
use think\facade\App;
use frank\base\BaseController;
use app\common\services\statistics\StatisticsOrderService;
use app\common\services\user\UserService;
use app\common\services\store\GoodsService;
use app\common\services\store\GoodsSkuService;

/**
 * Class Statistics
 */
class Statistics extends BaseController
{ 	
    /**
     * Config constructor.
     * @param App $app
     */
    public function __construct(App $app,StatisticsOrderService $service)
    {
        parent::__construct($app);
        $this->service = $service;
    }

	/**
     * @method(GET)
     */
    public function main()
    {
        $today = date('Y-m-d');
        $yesterday = date('Y-m-d', strtotime('-1 days'));
        $userService = UserService::instance();
        $data = [
        	// 订单数
            'order_count' => [
                'total' => $this->service->getOrderTotal(),
                'today' => $this->service->getOrderTotal($today),
                'yesterday' => $this->service->getOrderTotal($yesterday),
            ],
        	// 销售额
            'order_price_count' => [
                'total' => $this->service->getOrderTotalPrice(),
                'today' => $this->service->getOrderTotalPrice($today),
                'yesterday' => $this->service->getOrderTotalPrice($yesterday),
            ],
        	// 会员数
            'user_count' => [
                'total' => $userService->getCreateTotal(),
                'today' => $userService->getCreateTotal($today),
                'yesterday' => $userService->getCreateTotal($yesterday),
            ],
        ];
        // 待支付订单
        $data['order_pay'] = $this->service->getOrderTotalByHandle('pay');
        // 待发货订单
        $data['order_delivery'] = $this->service->getOrderTotalByHandle('delivery');
        // 售后中订单
        $data['order_refund'] = $this->service->getOrderTotalByHandle('refund');

        $goodsService = GoodsService::instance();
        $goodsSkuService = GoodsSkuService::instance();
        // 出售中商品
        $data['goods_status_sale'] = $goodsService->getGoodsStatusCount(1);
        // 仓库中商品
        $data['goods_status_stock'] = $goodsService->getGoodsStatusCount(0);
        // 库存预警
        $data['goods_stock_warning'] = $goodsSkuService->getGoodsStockWarning();
        $this->success($data);
    }
}
